package org.bendev.ildaplayer;

import java.util.List;

import android.app.Fragment;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

public class ChannelListFragment extends Fragment {
	
	public static interface OnChannelClickListener{
		public void onChannelClick( String channel );
	}
	
	AbsListView mListView;
	OnChannelClickListener mOnChannelClickListener;
	Object mLock = new Object();
	
	@Override
	public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
		View res = inflater.inflate( R.layout.fragment_channel_list, container );
		mListView = (AbsListView) res.findViewById( android.R.id.list );
		return res;
	}
	
	@Override
	public void onActivityCreated( Bundle savedInstanceState ){
		super.onActivityCreated( savedInstanceState );
		
		setListAdapter( new ArrayAdapter<String>( getActivity(), R.layout.channel_list_item, R.id.channel_text ){
			@Override
			public View getView( int position, View convertView, ViewGroup parent ){
				View res = super.getView( position, convertView, parent );
				final TextView tv = (TextView) res.findViewById( R.id.channel_text );
				final ImageView iv = (ImageView) res.findViewById( R.id.channel_logo );
				String channel = (String) getItem( position );
				RemoteBitmap.get( getActivity(), channel, Uri.parse( "http://www.rewindtv-dev.orange.fr/audioreco/channel/"+channel ), new RemoteBitmap.Callback() {
					@Override
					public void onBitmapError( Exception ex ) {}
					@Override
					public void onBitmap( Bitmap bm ) {
						iv.setImageBitmap( bm );
						tv.setVisibility( View.GONE );
					}
				} );
				return res;
			}
		} );
		
		getListView().setOnItemClickListener( new OnItemClickListener() {
			@Override
			public void onItemClick( AdapterView<?> parent, View view, int pos, long id ) {
				synchronized(mLock){
					if( mOnChannelClickListener!=null )
						mOnChannelClickListener.onChannelClick( (String) getListAdapter().getItem( pos ) );
				}
			}
		} );
	}
	
	public void setOnChannelClickListener( OnChannelClickListener l ){
		synchronized(mLock){ mOnChannelClickListener = l; }
	}
	
	@SuppressWarnings("unchecked")
	public void setChannels( List<String> channels ){
		ArrayAdapter<String> aas = (ArrayAdapter<String>) getListAdapter();
		aas.clear();
		( (ArrayAdapter<String>) getListAdapter() ).addAll( channels );
	}
	
	void setListAdapter( ListAdapter adapter ){ mListView.setAdapter( adapter ); }
	ListAdapter getListAdapter(){ return mListView.getAdapter(); }
	AbsListView getListView(){ return mListView; }
}
